#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 3e5 + 5;

int n, m;
int a[N];
int pos[N][2];
int b[N], c[N];
int f[N], g[N], e[N];
int mx[N << 2], tag[N << 2];
void pushup(int p) { mx[p] = max(mx[p << 1], mx[p << 1 | 1]); }
void addtag(int p, int d) {
  tag[p] += d;
  mx[p] += d;
}
void pushdown(int p) {
  if (tag[p]) {
    addtag(p << 1, tag[p]);
    addtag(p << 1 | 1, tag[p]);
    tag[p] = 0;
  }
}
void update(int L, int R, int d, int p, int l, int r) {
  if (L <= l && R >= r) {
    addtag(p, d);
    return;
  }
  pushdown(p);
  int mid = (l + r) >> 1;
  if (L <= mid) update(L, R, d, p << 1, l, mid);
  if (R > mid) update(L, R, d, p << 1 | 1, mid + 1, r);
  pushup(p);
}
int query(int L, int R, int p, int l, int r) {
  if (L <= l && R >= r) {
    return mx[p];
  }
  pushdown(p);
  int mid = (l + r) >> 1;
  int res = -1e9;
  if (L <= mid) res = max(res, query(L, R, p << 1, l, mid));
  if (R > mid) res = max(res, query(L, R, p << 1 | 1, mid + 1, r));
  return res;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) cin >> a[i];
  rep(i, 1, n) {
    f[i] = f[i - 1];
    if (!b[a[i]]) f[i]++, b[a[i]] = i;
  }
  per(i, n, 1) {
    g[i] = g[i + 1];
    if (!c[a[i]]) g[i]++, c[a[i]] = i;
  }
  rep(i, 1, n) {
    int x = a[i];
    if (!pos[x][0])
      pos[x][0] = i;
    else {
      if (pos[x][1]) update(pos[x][0], pos[x][1] - 1, -1, 1, 1, n);
      pos[x][1] = i;
      update(pos[x][0], pos[x][1] - 1, 1, 1, 1, n);
    }
    e[i] = mx[1];
  }
  int ans = -1e9;
  rep(i, 1, n - 1) ans = max(f[i] + e[i] + g[i + 1], ans);
  cout << ans;
  return 0;
}